package cn.flying.cloud.base.feign;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import feign.Feign;

/**
 * feign调用配置，作用是排除@RequestMapping注解在feign的接口类上时报路径重复注册问题
 *
 * @author: admin
 * @date: 2022年04月10日 14:54
 * @version: 1.0
 */
@Configuration
@ConditionalOnClass({Feign.class})
public class FeignConfiguration implements WebMvcRegistrations {
    @Override
    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return new FeignFilterRequestMappingHandlerMapping();
    }

    private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            // (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null)
            return super.isHandler(beanType) && !AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class);
        }
    }
}